#pragma once
#include "ui_mainWindow.h"
#include <QMainWindow>
#include <QMouseEvent>
#include <QDesktopWidget>
#include <QDebug>
#include <QStandardItemModel>
#include <QStandardItem>
#include <QModelIndex>
#include <QTimer>
#include <fstream>
#include <thread>
#include <chrono>
#include <QGridLayout>
#include <QMessageBox>
#include <QFileDialog>

#include "singleton.h"
#include "lv_api.h"
#include "Json.h"
#include "task.h"
#include "dockWidget.h"
#include "DxyThreadPool.h"

#define BOTTOMRIGHT     0
#define TOPRIGHT        1
#define TOPLEFT         2
#define BOTTOMLEFT      3
#define DOWN            4
#define LEFT            5
#define RIGHT           6
#define UP              7
#define NONE            8
#define MARGIN_MIN_SIZE 0
#define MARGIN_MAX_SIZE 6


inline int getMaxExposureFromFrameRate(int rate)
{
    if(15 < rate && rate <= 25)
    {
        return 1000;
    }
    else if(10 < rate && rate <= 15)
    {
        return 1300;
    }
    else if(5 < rate && rate <= 10)
    {
        return 2000;
    }
    else
    {
        return 4000;
    }
}


class mainWindow : public QMainWindow {
    Q_OBJECT
    
    friend class Task;
public:
    mainWindow(QWidget* parent = nullptr);
    ~mainWindow();

    bool event(QEvent *event);
    void mouseDoubleClickEvent(QMouseEvent * event);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void updateRegion(QMouseEvent *event);
    void resizeRegion(int marginTop, int marginBottom, int marginLeft, int marginRight);
    void mouseReleaseEvent(QMouseEvent *event);
    void leaveEvent(QEvent *event);
    void closeEvent(QCloseEvent * event);
    void statuCtl();
    void configInit();

private slots:
    void changeMinMax();
    void scanDevice();
    void on_treeView_clicked(const QModelIndex &index);
    void on_openClicked();
    void on_stopClicked();
    void renderData(int type);
    


private:
    Ui_mainWindow*          ui;
    bool                    m_isMax             = false;
    bool                    m_bIsPressed        = false;
    bool                    m_bIsResizing       = false;
    bool                    m_bIsDoublePressed  = false;
    uint8_t                 m_direction         = 0;    
    QPoint                  m_pressPoint;
    QPoint                  m_movePoint;
    Task *                  m_task              = nullptr;
    uint32_t                m_deviceIdx         = -1;
    QStandardItemModel *    m_model             = nullptr;

    QTimer *                m_timer             = nullptr;
};


#define CONNECTMINMAX(obj1, obj2, row, col) \
    connect(ui->obj1, &QPushButton::clicked, [this] () { \
        if(!ui->obj2->isFullScreen())\
        {\
            ui->obj2->setParent(nullptr);\
            ui->obj2->setWindowFlag(Qt::Window);\
            ui->obj2->showFullScreen();\
            m_task->setFullScreen(true);\
        }\
        else\
        {\
            ui->obj2->setParent(ui->widget_6);\
            ui->gridLayout_2->addWidget(ui->obj2, row, col);\
            ui->obj2->setWindowFlag(Qt::SubWindow);\
            ui->obj2->showNormal();\
            m_task->setFullScreen(false);\
        }\
    });\
